var app = getApp()
var network = require("../../api/api.js")
Page({
  data: {
    query: {},
    grounps: [],
    groupsLength: -1,
    already_groups: 0,
    timeTxt: '',
    total_number: 0,
    already_number: 0
  },
  onLoad: function (options) {
    var that = this
    this.setData({
      query: options
    })
    // this.getGroups()
    // this.get_paper_info()
  },
  onShow: function () {
    this.get_paper_info()
    this.getGroups()
  },
    onUnload: function () {
        if (this.data.setTimeOutLists) {
            clearTimeout(this.data.setTimeOutLists)
        }
        this.setData({
            setTime: 1
        })
    },
  getGroups: function () {
    const that = this
    var userLogin = wx.getStorageSync('user_login') || {}
    var params = new Object()
    params.business_param = {}
    params.business_param.user_id = String(userLogin.user_id)
    params.business_param.paper_id = this.data.query.paper_id
    params.business_param.user_paper_id = this.data.query.user_paper_id
    network.POST({
      params: params,
      url: "get_paper_groups",
      success: function (res) {
        let result = res.result.map(cur => {
          cur.process = Math.floor((cur.done_questions_num / cur.questions_num) * 100)
          return cur
        })
        that.setData({
          groups: res.result,
          groupsLength: res.result.length
        })
        //拿到解密后的数据，进行代码逻辑

      },
      fail: function () {
        //失败后的逻辑

      },
    })
  },
  get_paper_info(cb) {
    const that = this
      var userLogin = wx.getStorageSync('user_login') || {}
    var params = new Object()
    params.business_param = {}
    params.business_param.user_id = String(userLogin.user_id)
    params.business_param.paper_id = this.data.query.paper_id
    params.business_param.user_paper_id = this.data.query.user_paper_id
    params.business_param.submit_status = 1
    params.business_param.submit_type = 1
    params.business_param.answer_list = this.data.already_answer
    network.POST({
      params: params,
      url: "get_paper_info",
      success: function (res) {
        const result = res.result[0]
        that.setTimer(result.live_time_limit - 0)
        wx.setStorageSync('time_limit', result.time_limit || 90);
        that.setData({
          already_number: result.done_questions_num - 0,
          total_number: result.questions_num - 0
        })
        if (cb) {
          cb(result.done_questions_num + '', result.questions_num + '')
        }
        //拿到解密后的数据，进行代码逻辑
      },
      fail: function () {
        //失败后的逻辑
      },
    })
  },
  // 倒计时
  setTimer: function (m) {
    let that = this;
    let ts = m;
    let tid = 'tid';
    let timer = function (id) {
      if (ts > 0) {
        var hh = parseInt(ts / 60 / 60 % 24, 10);
        var mm = parseInt(ts / 60 % 60, 10);
        var ss = parseInt(ts % 60, 10);
        hh = hh < 10 ? ("0" + hh) : hh; //时
        mm = mm < 10 ? ("0" + mm) : mm; //分
        ss = ss < 10 ? ("0" + ss) : ss; //秒
        const setTimeOutLists = setTimeout(function () {
          timer(id);
        }, 1000);
        ts -= 1;
        that.setData({
          timeTxt: hh + ':' + mm + ':' + ss,
            setTimeOutLists: setTimeOutLists
        })
      } else {
        that.setData({
          timeTxt: '00:00:00'
        })
        that.finished_paper()
      }
    }
    timer(tid);
  },
  get_answer: function (e) {
    const groups_id = e.currentTarget.dataset.query.groupsid
      if (this.data.setTimeOutLists) {
          clearTimeout(this.data.setTimeOutLists)
      }
    wx.navigateTo({
      url: '../list/list?paper_id=' + this.data.query.paper_id + '&user_paper_id=' + this.data.query.user_paper_id + '&groups_id=' + groups_id
    })
  },
  finishedFn: function () {
    let that = this;
    this.get_paper_info((num, allNum) => {
      if (num !== allNum && that.data.timeTxt !== '00:00:00') {
        wx.showToast({
          title: '请先答完题目',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      that.finished_paper()
    })
  },
  finished_paper: function () {
    const that = this
    var userLogin = wx.getStorageSync('user_login') || {}
    var params = new Object()
    params.business_param = {}
    params.business_param.user_id = String(userLogin.user_id)
    params.business_param.paper_id = this.data.query.paper_id
    params.business_param.user_paper_id = this.data.query.user_paper_id
    network.POST({
      params: params,
      url: "finished_paper",
      success: function (res) {
        const result = JSON.stringify(res.result[0])
        wx.reLaunch({
          url: `../resu/resu?result=${result}&paper_id=${that.data.query.paper_id}&user_paper_id=${that.data.query.user_paper_id}`
        })
        //拿到解密后的数据，进行代码逻辑
      },
      fail: function () {
        //失败后的逻辑

      },
    })
  }
})